unit Unit4;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, CryptoAPI, unitMD5;

type
  TForm4 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    CheckBox1: TCheckBox;
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form4: TForm4;
  EXEPath: AnsiString;
  KeyExists: BOOL;

implementation

uses Unit1;

{$R *.dfm}

procedure TForm4.Button2Click(Sender: TObject);
begin
  ExitProcess(0);
end;

procedure TForm4.Button1Click(Sender: TObject);
var
  c: array [0 .. 256] of AnsiChar;
  r: DWORD;
  prow: array [0 .. 255] of AnsiChar;
  pch: PAnsiChar;
begin
  if KeyExists then
  begin
    FillChar(c, 256, 0);
    lstrcpyA(c, PAnsiChar(AnsiString(Edit1.Text)));
    FillChar(prow, 255, 0);
    lstrcpyA(prow, 'remote_trafficgate');

    pch := nil;
    if Form4.CheckBox1.Checked then
      pch := prow;
    r := ImportPair(EXEPath + 'Keyset\private.rsa', c, Form1.ProwLoc,
      Form1.KLoc, pch);

    if r <> 0 then
    begin
      ShowMessage('ImportPair failed (Password is incorrect?): ' +
        SysErrorMessage(r));
      ExitProcess(0);
    end;
  end
  else
  begin
    ShowMessage
      ('Key pair does''t exists. Generate new key before working (run wizard, kmgr.exe).');
    WinExec(PAnsiChar(EXEPath + 'KMgr.exe'), SW_SHOW);
    ExitProcess(0);
  end;
  Form1.Show;
  Form4.Hide;

  Form1.TrayIcon1.Visible := true;
end;

procedure TForm4.FormCreate(Sender: TObject);
var
  hFile: THandle;
  Creation, Write, Access: FileTime;
  sCreation, sWrite, sAccess: _SYSTEMTIME;
  strWrite, strAccess: String;
  Buf: Pointer;
  F: File;
  FSize: DWORD;
begin
  // If you want a peace...
  EXEPath := AnsiString(ExtractFilePath(Application.ExeName));
  // ...Prepare for war!

  hFile := CreateFileA(PAnsiChar(EXEPath + 'Keyset\private.rsa'), GENERIC_READ,
    0, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  KeyExists := (hFile <> $FFFFFFFF);
  if KeyExists then
  begin
    GetFileTime(hFile, @Creation, @Access, @Write);
    FileTimeToSystemTime(Creation, sCreation);
    FileTimeToSystemTime(Write, sWrite);
    FileTimeToSystemTime(Access, sAccess);

    DateTimeToString(strWrite, 'DDDDDD hh:mm:ss', SystemTimeToDateTime(sWrite));
    DateTimeToString(strAccess, 'DDDDDD hh:mm:ss',
      SystemTimeToDateTime(sAccess));

    Form4.Label4.Caption := strWrite;
    Form4.Label5.Caption := strAccess;
    Form4.Label6.Caption := '';

    CloseHandle(hFile);

    AssignFile(F, String(EXEPath + 'Keyset\private.rsa'));
    Reset(F, 1);
    FSize := FileSize(F);
    Buf := GetMemory(FSize);
    BlockRead(F, Buf^, FSize);
    CloseFile(F);

    Label6.Caption := String(TestMD5(Buf, FSize));

    FreeMemory(Buf);
  end;
end;

end.
